﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace ArkanoidX.UI
{
    public class Editor : Window
    {
        public String Value = "";
        public int CursorPos = 0;
        public SpriteFont Font = null;
        public Texture2D Cursor;
        public int MaxLen = 40;
        float _scale = 1;

        public Editor(MainGame game)
            : base(game)
        {
            Font = game.Content.Load<SpriteFont>("Levels");    // default font
            Cursor = game.Content.Load<Texture2D>("EditorCursor");
            background = game.Content.Load<Texture2D>("editor");

            canBeFocused = true;
            canBeActivated = true;
        }

        public override void SetRectangle(Rectangle rect)
        {
            // fixed-width font assumed
            Vector2 fontSize = Font.MeasureString("A");
            _scale = rect.Height / fontSize.Y;

            float fontWidth = fontSize.X * _scale;
            if (MaxLen > rect.Width / fontWidth)
            {
                MaxLen = (int)Math.Round(rect.Width / fontWidth);
            }

            base.SetRectangle(rect);
        }

        public override void Align(Rectangle rect, AlignType type, Vector2 size, 
            float xMargin, float yMargin)
        {
            Vector2 position = base.CalcAlign(rect, type, size, xMargin, yMargin);
            Vector2 halfSize = size * 0.5f;
            position -= halfSize;
            SetRectangle(new Rectangle((int)position.X, (int)position.Y, 
                (int)size.X, (int)size.Y));
        }

        public override void TryHandleKeyEvent(GameTime gameTime, Window.KeyStrokeEvent e)
        {
            if (CursorPos < MaxLen)
            {
                if (e.key >= Keys.A && e.key <= Keys.Z)
                {
                    char ch = (char)('A' + (e.key - Keys.A));
                    Value = Value + ch;
                    CursorPos = Value.Length;
                    e.consumed = true;
                }
                else if (e.key >= Keys.D0 && e.key <= Keys.D9)
                {
                    char ch = (char)('0' + (e.key - Keys.D0));
                    Value = Value + ch;
                    CursorPos = Value.Length;
                    e.consumed = true;
                }
            }

            if (e.key == Keys.Back)
            {
                if (Value.Length > 0)
                {
                    Value = Value.Substring(0, Value.Length - 1);
                    CursorPos = Value.Length;
                    e.consumed = true;
                }
            }

            base.TryHandleKeyEvent(gameTime, e);
        }

        public override void Update(GameTime gameTime)
        {
            KeyboardState kbs = Keyboard.GetState();

            if (parent != null)
            {
                FontColor = parent.FontColor;
                DrawColor = parent.DrawColor;
            }

            if (state != State.Running)
            {
                base.Update(gameTime);
                return;
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);

            SpriteBatch sBatch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
            if (focused && gameTime.TotalGameTime.Milliseconds % 1000 < 500)
            {
                int x;
                if (!string.IsNullOrEmpty(Value))
                {
                    Vector2 size = Vector2.Zero;
                    size = Font.MeasureString(Value.Substring(0, CursorPos));
                    x = (int)Math.Round(boundingRect.X + size.X * _scale + 2);
                }
                else
                {
                    x = boundingRect.X + 2;
                }

                Rectangle cursorRect = new Rectangle(x, boundingRect.Y, 4, boundingRect.Height);
                sBatch.Draw(Cursor, cursorRect, Color.White);

            }
            sBatch.DrawString(Font, Value, new Vector2(boundingRect.Left, boundingRect.Top), Color.Blue, 0, 
                new Vector2(0, 0), _scale, SpriteEffects.None, 0);
        }
    }
}
